package com.study.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.study.model.jdao.ICustomerDAO;
import com.study.model.jdao.ICustomerconsolidationDAO;
import com.study.model.jdao.ILinkmanDAO;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Customerconsolidation;
import com.study.model.pojo.Linkman;
import com.study.model.pojo.Orderform;

/**
 * 查询客户合并信息
 * 
 * @author 郭星
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CustormAll {

	@Autowired
	ICustomerconsolidationDAO icustmerconsolidationdao;
	@Autowired
	ICustomerDAO icustomerdaos;
	@Autowired
	ILinkmanDAO ilinkmandaos;

	/**
	 * 查询合并后客户的信息
	 * 
	 * @param     <T>
	 * @param ids 客户id
	 * @param dos 做什么
	 * @return
	 */
	public <T> List<T> findAll(int ids, int dos) {
		System.out.println("来到了公用方法：");
		List<Integer> list = new ArrayList<>();
		list.add(ids);
		// 查询合并表的记录
		List<Customerconsolidation> findById = icustmerconsolidationdao.findBytCuctIdes(ids);
		System.out.println("这里没出错：" + findById);
		if (findById != null && findById.size() > 0) {
			for (Customerconsolidation d : findById) {
				System.out.println("----" + d);
				list.add(d.getCustomer2().getCusId());
			}

		}
		System.out.println("到这里了");

		switch (dos) {
		case 1:
			// 查询联系人
			List<Linkman> findAllLinkMan = findAllLinkMan(list);
			for (Linkman linkman : findAllLinkMan) {
				System.out.println(linkman);
			}
			return (List<T>) findAllLinkMan;

		case 2:
			// 查询客户ID
			return (List<T>) list;

		default:
			break;
		}
		return null;

	}

	/**
	 * 查询对应合并后的几个客户id的联系人
	 * 
	 * @param ids
	 * @return
	 */
	private List<Linkman> findAllLinkMan(List<Integer> list) {
		List<Linkman> list2 = new ArrayList<>();
		for (Integer integer : list) {
			list2.addAll(ilinkmandaos.findBycustomers(integer));
		}
		return list2;
	}

}
